package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/count-and-say/">38. 外观数列</a>
 */
public class Solution38 extends BaseSolution {

    public String countAndSay(int n) {
        String rst = "1";
        while (n-- > 1) {
            char[] vals = rst.toCharArray();
            int p = 1, len = 1;
            char last = vals[0];
            StringBuilder sb = new StringBuilder();
            while (p < vals.length) {
                if (vals[p] == last) {
                    len++;
                } else {
                    sb.append(len).append(last);
                    last = vals[p];
                    len = 1;
                }
                p++;
            }
            sb.append(len).append(last);
            rst = sb.toString();
        }
        return rst;
    }

    public static void main(String[] args) {
        Solution38 solution = new Solution38();
        System.out.println(solution.countAndSay(4));
    }
}